<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 2017/4/25
 * Time: 10:56
 */

namespace app\weixin\controller;


use app\common\controller\LiteBase;
use app\common\model\DonationComment;
use app\common\util\DateTimeUtil;
use app\common\util\LoginManager;

class Coral extends LiteBase
{
    public function index()
    {
        $corals = \app\common\model\Coral::instance()
            ->where(['member_id' => LoginManager::getLoginUserId(), 'title' => ''])
            ->select();
        $this->assign('corals', $corals);
        return $this->fetch();
    }

    public function ranklist()
    {
        $m = \app\common\model\Coral::instance()->getRankList();
        $donation = DonationComment::instance();
        foreach ($m as &$item) {
            $item['days'] = $item['days'] > 180 ? 180 : $item['days'];
            $item['msg'] = $donation->where(['member_id' => $item['member_id']])->order('id desc')->getField('content');
        }
        $this->assign('ranklist', $m);
        return $this->fetch();
    }

    public function ranklist2()
    {
        $m = \app\common\model\Coral::instance()->getRankList(1);
        $donation = DonationComment::instance();
        foreach ($m as &$item) {
            $item['days'] = $item['days'] > 180 ? 180 : $item['days'];
            $item['msg'] = $donation->where(['member_id' => $item['member_id']])->order('id desc')->getField('content');
        }
        $this->assign('ranklist', $m);
        return $this->fetch();
    }

    public function rename()
    {
        if (!empty($_POST['names'])) {
            $names = $_POST['names'];
            if (!empty($names) && is_array($names)) {
                $member_id = LoginManager::getLoginUserId();
                $instance = \app\common\model\Coral::instance();
                foreach ($names as $id => $name) {
                    if ($instance->where(['id' => $id, 'member_id' => $member_id, 'title' => ''])->count()) {
                        $instance->where(['id' => $id])->save([
                            'title' => $name,
                            'name_time' => DateTimeUtil::now()
                        ]);
                    }
                }
                return $this->jsonSuccess();
            }
        }
        return $this->jsonFail('您没有需要命名的珊瑚');
    }

    public function home()
    {
        $info = LoginManager::getLoginUserInfo();
        $this->assign('info', $info);
        $days = \app\common\model\Coral::instance()->getCoralPassedDays($info['id']);
//        $days = 5 * 30 + 1;
        $maxWeight = 0.01 * 180; // kg
        $status = 0;
        if ($days > 0) {
            if ($days > 180) {
                $days = 180;
            }
            $status = ceil($days / 30);
        }
        $this->assign('maxWeight', $maxWeight);
        $this->assign('status', $status);
        $this->assign('weight', $days * 0.01 > 0 ? $days * 0.01 : 0);
        return $this->fetch();
    }

    public function help()
    {
        return $this->fetch();
    }
}